﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TCCVoigt.Models;

namespace TCCVoigt.Controllers
{
    public class RestricaoController : Controller
    {
        private TCCVoigtEntities db = new TCCVoigtEntities();

        //
        // GET: /Restricao/

        public ActionResult Index()
        {
            var restricao = db.Restricao.Include("TipoRestricao");
            return View(restricao.ToList());
        }

        //
        // GET: /Restricao/Details/5

        public ActionResult Details(int id = 0)
        {
            Restricao restricao = db.Restricao.Single(r => r.Codigo == id);
            if (restricao == null)
            {
                return HttpNotFound();
            }
            return View(restricao);
        }

        //
        // GET: /Restricao/Create

        public ActionResult Create()
        {
            ViewBag.CodigoTipoRestricao = new SelectList(db.TipoRestricao, "Codigo", "Descricao");
            ViewBag.CodigoRecurso = new SelectList(db.Recurso.OrderBy(p => p.Descricao), "Codigo", "Descricao");
            return View();
        }

        //
        // POST: /Restricao/Create

        [HttpPost]
        public ActionResult Create(Restricao restricao)
        {
            if (ModelState.IsValid)
            {
                db.Restricao.AddObject(restricao);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CodigoTipoRestricao = new SelectList(db.TipoRestricao, "Codigo", "Descricao", restricao.CodigoTipoRestricao);
            ViewBag.CodigoRecurso = new SelectList(db.Recurso.OrderBy(p => p.Descricao), "Codigo", "Descricao", restricao.CodigoRecurso);
            return View(restricao);
        }

        //
        // GET: /Restricao/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Restricao restricao = db.Restricao.Single(r => r.Codigo == id);
            if (restricao == null)
            {
                return HttpNotFound();
            }
            ViewBag.CodigoTipoRestricao = new SelectList(db.TipoRestricao, "Codigo", "Descricao", restricao.CodigoTipoRestricao);
            ViewBag.CodigoRecurso = new SelectList(db.Recurso.OrderBy(p => p.Descricao), "Codigo", "Descricao", restricao.CodigoRecurso);
            return View(restricao);
        }

        //
        // POST: /Restricao/Edit/5

        [HttpPost]
        public ActionResult Edit(Restricao restricao)
        {
            if (ModelState.IsValid)
            {
                db.Restricao.Attach(restricao);
                db.ObjectStateManager.ChangeObjectState(restricao, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CodigoTipoRestricao = new SelectList(db.TipoRestricao, "Codigo", "Descricao", restricao.CodigoTipoRestricao);
            ViewBag.CodigoRecurso = new SelectList(db.Recurso.OrderBy(p => p.Descricao), "Codigo", "Descricao", restricao.CodigoRecurso);
            return View(restricao);
        }

        //
        // GET: /Restricao/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Restricao restricao = db.Restricao.Single(r => r.Codigo == id);
            if (restricao == null)
            {
                return HttpNotFound();
            }
            return View(restricao);
        }

        //
        // POST: /Restricao/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Restricao restricao = db.Restricao.Single(r => r.Codigo == id);
            db.Restricao.DeleteObject(restricao);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}